-
Notifications
You must be signed in to change notification settings - Fork 1
/
part.ts
48 lines (39 loc) · 1.19 KB
/
part.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** @module Route.Geometry.[id].Part */
import { Request, Response } from 'express'
import { session } from '@/route/session'
import { checkGeometryAuth } from '@/route/auth'
import { error } from '@/route/error'
import GeometryLib from '@/lib/geometry'
/**
* Geometry API part
* @param req Request
* @param res Response
*/
const part = async (req: Request, res: Response) => {
try {
// Check session
const sessionId = await session(req)
// Id
const id = req.query.id ?? req.params.id // Electron
// Check
if (!id || typeof id !== 'string')
throw error(400, 'Missing data in your request (query: { id(string) })')
// Check authorization
await checkGeometryAuth({ id: sessionId }, { id })
if (req.method === 'GET') {
try {
// Download
const partContent = await GeometryLib.readPart({ id })
res.status(200).json(partContent)
} catch (err: any) {
throw error(500, err.message)
}
} else {
// Unauthorized method
throw error(402, 'Method ' + req.method + ' not allowed')
}
} catch (err: any) {
res.status(err.status).json({ error: true, message: err.message })
}
}
export default part